# -*- coding:utf-8 -*-
# @File       :   conftest.py
# @Author     :   LINING
# @Time       :   2024/7/22 16:02
# @Description :  用例执行前后执行后的一些操作，pgc登录token存在cookie，saas存在sessionStorage，
# 自带的方法不能取到sessionStorage，用js获取和插入,pgc存在cookie使用自带方法即可
import os
from TestCase.PageObject.login.login_pgc_user import LoginPgcUser
import pytest
from Handle.handle_log import log
from playwright.sync_api import Page, BrowserType
from TestCase.PageObject.login.login_saas import LoginSaas
from Handle.handle_yaml import domain, account_user, Test_File
from typing import Dict



@pytest.fixture(scope='session')
def login_saas_fixture():
    """
    saas执行前登录获取token,pgc使用cookie
    :return:
    """
    pass


# pgc创作者登录获取token
@pytest.fixture(scope="function")
def pgc_user_login(
    browser_type: BrowserType,
    browser_type_launch_args: Dict,
    browser_context_args: Dict
):
    # 保存在文件中
    context = browser_type.launch_persistent_context(Test_File, **{
        **browser_type_launch_args,
        **browser_context_args,
        "locale": "de-DE",
    })
    page = context.pages[0]
    #page.goto(f'{domain}#/login')
    #LoginPgcUser(page).login_success_user(account_user[0], account_user[1])
    yield page
    page.close()
    context.close()


# @pytest.fixture(scope='function')
# def pgc_login_user(page:Page):
#     # 执行js插入cookie
#     page.goto(domain)
#     with open(token_file+'_user', 'r', encoding='utf-8') as f:
#         cookie = f.read()
#     cookie = cookie.split(';')
#     cookie_dict = {'vue_pgc_page_rights':'', 'vue_pgc_role':'', 'vue_pgc_token':''}
#     for i in cookie:
#         key = i.split('=')[0].strip()
#         if key in cookie_dict:
#             value = i.split('=')[1]
#             cookie_dict[key] = value
#     log.log_info('pgc创作者token'+str(cookie_dict))
#     #LoginPgcUser(page).click_login_button()
#     set_cookies(page, cookie_dict)
#     yield page
#     page.close()




